package net.xdclass.demo.service.impl;

import net.xdclass.demo.controller.request.VisitRecordPageRequest;
import net.xdclass.demo.controller.request.VisitTrendQueryRequest;
import net.xdclass.demo.mapper.VisitStatsMapper;
import net.xdclass.demo.model.VisitStatsDO;
import net.xdclass.demo.service.VisitStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小滴课堂,愿景：让技术不再难学
 *
 * @Description
 * @Author 二当家小D
 * @Remark 有问题直接联系我，源码-笔记-技术交流群
 * @Version 1.0
 **/

@Service
public class VisitStatsServiceImpl implements VisitStatsService {

    @Autowired
    private VisitStatsMapper visitStatsMapper;


    @Override
    public Map<String, Object> pageVisitRecord(VisitRecordPageRequest pageRequest) {

        Map<String,Object> data = new HashMap<>(3);

        Long productId = pageRequest.getProductId();
        int page = pageRequest.getPage();
        int size = pageRequest.getSize();

        int count = visitStatsMapper.countTotal(productId);

        int from = (page -1) * size;

        List<VisitStatsDO> visitStatsDOS = visitStatsMapper.pageVisitRecord(productId, from, size);

        data.put("total",count);
        data.put("current_page",page);
        data.put("data",visitStatsDOS);

        /**
         * 计算总页数
         */
        int totalPage;
        if(count % size == 0){
            totalPage = count / size;
        }else {
            totalPage = count / size +1 ;
        }

        data.put("total_page",totalPage);
        return data;
    }

    @Override
    public List<VisitStatsDO> queryVisitTrend(VisitTrendQueryRequest queryRequest) {

        Long productId = queryRequest.getProductId();

        String type = queryRequest.getType();

        List<VisitStatsDO> list = null;

        if(type.equalsIgnoreCase("region")){

            list = visitStatsMapper.queryRegionTrendWithMultiDay(productId,queryRequest.getStartTime(),queryRequest.getEndTime());

        } else if(type.equalsIgnoreCase("day")){

            list = visitStatsMapper.queryVisitTrendWithMultiDay(productId,queryRequest.getStartTime(),queryRequest.getEndTime());
        }



        return list;
    }
}
